package com.example.app2;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class ActivityList2 extends AppCompatActivity implements Runnable, AdapterView.OnItemClickListener {
    private static final String TAG = "测试";
    Handler handler;
    ListView listView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list2);

        listView = findViewById(R.id.list2);

        handler = new Handler(Looper.myLooper()){
            @Override
            public void handleMessage(Message msg) {
                if(msg.what==2){
                    ArrayList<HashMap<String,String>> list = (ArrayList<HashMap<String,String>>)msg.obj;
                    MyAdapter adapter = new MyAdapter(ActivityList2.this,R.layout.listitem_map,list);
                    listView.setAdapter(adapter);
                }

                super.handleMessage(msg);
            }
        };
        listView.setOnItemClickListener(this);

        Thread t = new Thread(this);
        t.start();

    }


    @Override
    public void run() {
        try {
            Thread.sleep(1000);                      //设置子线程的延迟时间
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        Message msg = handler.obtainMessage(2);
        Document doc = null;
        try {
            doc = Jsoup.connect(" https://www.boc.cn/sourcedb/whpj/").get();   //使用jsoup爬取网页上的数据，得到一个doc
            Elements tables = doc.getElementsByTag("table");                  // getElementsByTag 获得标签信息，返回一个Elements对象
            Element table = tables.get(1);
            Elements tds = table.getElementsByTag("td");
            ArrayList<HashMap<String,String>> listItems = new ArrayList<>();
            for (int i = 0; i < tds.size(); i += 8) {
                Element coin = tds.get(i);
                Element getWebRate = tds.get(i + 5);
                Float webRate = Float.parseFloat(getWebRate.text())/100f;


                HashMap<String,String> map = new HashMap<>();
                map.put("coin",coin.text());
                map.put("rate",webRate.toString());
                listItems.add(map);

            }
            msg.obj = listItems;
            handler.sendMessage(msg);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Object Ob = listView.getItemAtPosition(position);
        HashMap<String,String> map = (HashMap<String, String>) Ob;
        String money = map.get("coin");
        String rate = map.get("rate");

        Intent intent = new Intent(this,Activity_list2_item.class);
        intent.putExtra("money",money);
        intent.putExtra("rate",Float.parseFloat(rate));
        startActivity(intent);                   //进入下一个页面


    }

}


















